/*#################################################

Popup | v1.6.6
Max Felker | max@bigroomstudios.com

Extensible pop class with static / iframe content 
support

#################################################*/

// START Class
var Popup = Class.create({

	initialize: function(config) {
		
		// if popup exists, exit script
		if(live.popups.get('popup-'+config.container)) { 		
			return false;  
		}
		
		// if no container, auto increment for id
		if(!config.container) {
			config.container = live.popups.keys().length+1;
		}
		
		// set id src and ittle
		this.popup_id = 'popup-'+config.container;
		this.src = config.src ? config.src : false;
		this.title = config.title ? config.title : '';
		
		// width and height
		if(config.fullscreen) {
			this.fullscreen = config.fullscreen;
			this.width = live.viewport.width-50;
			this.height = live.viewport.height-50;
		} else {
			this.width = config.width ? config.width : live.viewport.width/2;
			this.height = config.height ? config.height : live.viewport.height/2;
		}
		
		// set postion ( top / left or default to viewport - width / 2)
		this.position = {
			top: config.top ? config.top : (live.viewport.height-this.height)/2,
			top_static: config.top ? true : false,
			left: config.left ? config.left : (live.viewport.width-this.width)/2, 
			left_static: config.left ? true : false
		}
		
		// static dimensions (should it resize relative to the page)	
		this.static_dimensions = config.width && config.height ? true : false;
		
		// title height
		this.title_height = config.title_height ? config.title_height : 37;

		// offset
		this.offset = config.offset ? config.offset : {top:50,left:50};
	
		// popup options
		this.confirm_close = config.confirm_close ? config.confirm_close : false;
		this.drag = config.drag ? config.drag : false;
		this.minimize_window = config.minimize ? config.minimize : false;
	
		// build container
		this.container = Builder.node('div', { id: this.popup_id, className: "popup-container" }, [
			Builder.node('div', { id: this.popup_id+"-controls", className: "popup-controls wrapper" }, [
				Builder.node('p', { id: this.popup_id+"-title", className: "popup-title" }),
				Builder.node('a', { id: this.popup_id+"-close", className: "popup-close" }, 'x')
				])
		]);

		// style container
		this.container.setStyle({
			position: "absolute",
			width: this.width+"px",
			height: this.height+"px",
			top: this.position.top +"px",
			left: this.position.left+"px"
		});

		// if iframe popup
		if(this.src) {
		
			// build iframe
			this.iframe = Builder.node('iframe', { 
				id: this.popup_id+"-iframe", 
				name: this.popup_id+"-iframe",
				className:'popup-iframe', 
				frameborder: 0, 
				scrolling: "auto", 
				src: this.src
			});
		
			this.container.appendChild(this.iframe);
		
		// else content popup
		} else {
		
			this.content = Builder.node('div', { 
				id: this.popup_id+"-content", 
				className:'popup-content'
			});
		
			this.container.appendChild(this.content);
			
			this.content.update(config.content);
		
		}
		
		// if className was passed
		if(config.className) {
			this.container.addClassName(config.className);
		}
		
		// add popup container to body
		document.body.appendChild(this.container);
		
		// define popup container
		this.container = $(this.popup_id);
		
		// add to live
		live.popups.set(this.popup_id,this);
		
		// set controls container and set title height
		this.controls_container = $(this.popup_id+"-controls");
		this.controls_container.setStyle({
			height:this.title_height+'px'
		});
		
		// set title container, update it
		this.title_container =  $(this.popup_id+'-title');
		this.title_container.update(this.title);
		
		// is it iframe popup or content
		if(this.iframe) {
			this.iframe = $(this.popup_id+'-iframe');
		} else {
			this.content = $(this.popup_id+'-content');
		}	
		
		// define buttons
		this.btn = {
			close: $(this.popup_id+'-close')
		}	
		this.btn.close.on('click', this.close.bind(this));
	
		// if minimize window is true
		if(this.minimize_window) {
		
			this.btn.minimize = Builder.node('a', { id: this.popup_id+"-minimize", className: "popup-minize" }, '_');
			this.controls_container.appendChild(this.btn.minimize);
			this.btn.minimize = $(this.popup_id+'-minimize');	
			this.btn.minimize.on('click', this.minimize.bind(this));
		}

		// if drag is enable
		if(this.drag) {
			
			$(this.popup_id+"-controls").setStyle("cursor:move;");
			
			new Draggable(this.container, { 
				handle: this.container.id+'-controls',
				starteffect:'', 
				endeffect:''
			});
		
		} 
		
		// set resize on window resize
		Event.on(window,'resize', this.reposition.bind(this));
		
		// position popup
		this.reposition();
		
	},
	
	resize: function() {
	
		if(this.fullscreen) {
	
			this.width = live.viewport.width-50;
			this.height = live.viewport.height-50;
	
			this.container.setStyle({
				width: this.width+"px",
				height: this.height+"px"
			});
		
	
		} else {
	
			this.width = live.viewport.width/2;
			this.height = live.viewport.height/2;
	
			this.container.setStyle({
				width: this.width+"px",
				height: this.height+"px"
			});

		}
		
		this.resize_contents();
	
	},
	
	resize_contents: function() {
	
		if(this.iframe) {

			this.iframe.setStyle({
				width: this.width+"px",
				height: (this.height-this.title_height)+"px"
			});
		
		} else {
		
		
			this.content.setStyle({
				width: this.width+"px",
				height: (this.height-this.title_height)+"px"
			});
		
		}
	
	},
	
	reposition: function() {
	
		if(!this.static_dimensions) {

			this.resize();
		
		} else {
		
			this.resize_contents();
		
		}	
		
		if(!this.position.top_static) {
			this.position.top = (live.viewport.height-this.height)/2;
			if(this.position.top<this.offset.top) {
				this.position.top = this.offset.top;
			}
		}
		
		if(!this.position.left_static) {
			this.position.left = (live.viewport.width-this.width)/2;
			if(this.position.left<this.offset.left) {
				this.position.left = this.offset.left;
			}
		}

	
		this.container.setStyle({
			top: this.position.top+"px",
			left: this.position.left+"px"
		});

	},

	show_popup: function() {

		new Effect.Appear(this.container, { duration: 0.3 });

	},

	minimize: function() { 

		if(this.iframe) {
			this.iframe.hide();
		} else {
			this.content.hide();
		}
		
		this.container.setStyle({ 
			height: "auto"
			
		});
		
		this.btn.minimize.update("+");
		
		this.btn.minimize.stopObserving();
		
		this.btn.minimize.on('click', this.restore.bind(this));
		
	},

	restore: function() { 

		if(this.iframe) {
			this.iframe.show();
		} else {
			this.content.show();
		}
		
		this.container.setStyle({ 
			width: this.width+"px",
			height: this.height+"px"		
		});
		
		this.btn.minimize.update("_");
		this.btn.minimize.stopObserving();
		this.btn.minimize.on('click', this.minimize.bind(this));
	
	},

	close: function() {
			
		if(this.confirm_close) {
			
			var user_confirm = confirm("Are you sure you would like to close this window?");
			if(user_confirm) {

				this.remove_popup();
				
			} else {
				return false;
			}
		} else {
		
			this.remove_popup();
			
		}
	
	},

	remove_popup: function() {
			
		Effect.Fade(this.container, {duration:0.25}); 		
	
		setTimeout(function() {
			this.container.remove();
			live.popups.unset(this.popup_id);
		}.bind(this), 350) ;
	
	},
		
	update_title: function(title) {
	
		this.title = title;
		this.title_container.update(title);
	
	}

}); 
